package com.leetcode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No17
 * @description 17. 电话号码的字母组合
 * @date 2021/3/13 下午5:52
 */
public class No17 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please Input Number(Only contians number from 2 to 9):");
        String input = br.readLine();
        List<String> res = letterCombinations(input);
        print(res);
        br.close();
    }

    public static List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if(digits == null || digits.equals("") || digits.length() == 0){
            return res;
        }
        String[] letters = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        doCombination(new StringBuilder(),res,letters,digits);
        return res;
    }

    private static void doCombination(StringBuilder stringBuilder, List<String> res, String[] letters, String digits) {
        if(stringBuilder.length() == digits.length()){
            res.add(stringBuilder.toString());
            return;
        }
        // 获取数字位置
        int currentDigits = digits.charAt(stringBuilder.length()) - '0';
        // 获取对应的字母
        String currentLetters = letters[currentDigits];
        for(char c:currentLetters.toCharArray()){
            stringBuilder.append(c);
            doCombination(stringBuilder,res,letters,digits);
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        }
    }

    private static void print(List<String> list){
        int len = list.size();
        for (int i = 0; i < len; i++) {
            if(i != len - 1){
                System.out.print(list.get(i));
                System.out.print(" ");
            }else{
                System.out.println(list.get(i));
            }
        }
    }

}
